<?php
namespace app\index\model;

use think\Image;
use think\Model;
use think\Db;
use think\Request;

class WithdrawalRecord extends Model
{
    const WITHDRAWAL_STATUS_PENDING = 'pending';// 申请处理中，
    const WITHDRAWAL_STATUS_SUCCESS = 'success'; // 成功过
    const WITHDRAWAL_STATUS_FIELD = 'failed'; // 失败
    const STATUS = [
        self::WITHDRAWAL_STATUS_PENDING => '申请成功等待处理中',
        self::WITHDRAWAL_STATUS_FIELD => '打款失败已退回',
        self::WITHDRAWAL_STATUS_SUCCESS => '打款成功'
    ];
    # 支付宝提现
    public function aliWithdrawal($user_id,$ali_account,$amount,$real_name)
    {
        Db::startTrans();
        $data = WithdrawalRecord::create([
            'user_id' => $user_id,
            'ali_account' => trim($ali_account),
            'amount' => $amount*1,
            'real_name' => trim($real_name),
            'type' => 'alipay'
        ]);
        $user = User::where('id', $user_id)->find();
        $user->wallet =  $user->wallet- $amount*100;
        $user->save();
        Db::commit();
        return $data;
    }

    public function bankWithdreawal($user_id,$amount,$real_name,$bank_name,$bank_cardid,$bank_open_address)
    {
        Db::startTrans();
        $data = WithdrawalRecord::create([
            'user_id' => $user_id,
            'bank_name' => trim($bank_name),
            'bank_cardid' => trim($bank_cardid),
            'bank_open_address' => trim($bank_open_address),
            'amount' => $amount*1,
            'real_name' => trim($real_name),
            'type' => 'bank'
        ]);
        $user = User::where('id', $user_id)->find();
        $user->wallet =  $user->wallet- $amount*100;
        $user->save();
        Db::commit();
        return $data;
    }


    # admin
    public static  function getlist(Request $request)
    {
        $genData = $request->get();
        $genData = getOutpageData($genData);
        if (isset($genData['status'])==='') {
            unset($genData['status']);
        }



        $times = '';
        if (isset($genData['create_time'])) {
            $times = explode(' - ', $genData['create_time']);
            unset($genData['create_time']);
        }



//        dd($genData);
        if (count($genData)>=2||isset($genData['searchval'])) {
            $fileds = ['amount',
                'id',
                'type',
                'real_name',
                'ali_account',
                'bank_cardid',
                'bank_open_address',
                'bank_name',
                'status',
                'user_remark',
                'admin_remark',
                'create_time',
                'update_time'
            ];
            $whereData = genWhereOrData($genData,$fileds);

            $model= makeWhereObject((new self()), $whereData);
        } else {
            $model=  (new self())->where($genData);

        }

        if ($times) {
            $model->whereBetween('create_time', $times);
        }
        $res = $model->order('id','desc')
//            ->fetchSql(true)
//            ->select();
            ->paginate(10, false,[  'var_page' => 'page']);
        return $res;
    }


}
